package com.ty.service;
import cn.smart.core.util.UserContext;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ty.dao.ShelfDao;
import com.ty.dao.WarehouseDao;
import com.ty.entity.Shelf;
import com.ty.entity.ShelfQuery;
import com.ty.entity.Warehouse;
import com.ty.entity.WarehouseQuery;
import com.ty.enums.ShelfStatus;
import com.ty.enums.WarehouseStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service//组件
public class ShelfService {

    @Autowired
    private ShelfDao shelfDao;

    public List<Shelf> select(ShelfQuery query) {
        LambdaQueryWrapper<Shelf> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(query.getWarehouseId()  != null, Shelf::getWarehouseId, query.getWarehouseId());
        queryWrapper.eq(query.getStatus()  != null, Shelf::getStatus, query.getStatus());
        List<Shelf> list = shelfDao.selectList(queryWrapper);
        return list;
    }

    public int insert(Shelf shelf) {

        return shelfDao.insert(shelf);
    }

    public int update(Shelf shelf) {
        LambdaUpdateWrapper<Shelf> updateWapper = Wrappers.lambdaUpdate(Shelf.class);
        updateWapper.set(shelf.getStatus() != null, Shelf::getStatus, shelf.getStatus());
        updateWapper.set(shelf.getName() != null, Shelf::getName, shelf.getName());
        updateWapper.set(shelf.getBrief() != null, Shelf::getBrief, shelf.getBrief());
        updateWapper.set(shelf.getSeq() != null, Shelf::getSeq, shelf.getSeq());
        updateWapper.set( Shelf::getLastUpdateBy, UserContext.get().getNickName());
        updateWapper.eq( Shelf::getId, shelf.getId());
        return shelfDao.update(updateWapper);
    }
    public int delete(Integer id) {
        LambdaUpdateWrapper<Shelf> updateWapper = Wrappers.lambdaUpdate(Shelf.class);
        updateWapper.set( Shelf::getStatus, ShelfStatus.OFF_SHELF.getCode());
        updateWapper.eq( Shelf::getId,id);
        int update = shelfDao.update(updateWapper);
        return update;
    }

    public List<Map> selectTree() {

        return   shelfDao.selectTree();
    }
}
